package com.atguigu.controller;

import com.atguigu.pojo.Dict;
import com.atguigu.pojo.Result;
import com.atguigu.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {

    private static final String PAGE_INDEX = "dict/index";
    @Autowired
    DictService dictService;

    @RequestMapping
    public String index() {
        return PAGE_INDEX;
    }


    @PostMapping("/test")
    @ResponseBody
    public String test(){
        dictService.findAllTree();
        return "123";
    }
    /// ///
    /**
     * [
     * { id:'011',	name:'n1.n1',	isParent:true},
     * { id:'012',	name:'n1.n2',	isParent:false},
     * { id:'013',	name:'n1.n3',	isParent:true},
     * { id:'014',	name:'n1.n4',	isParent:false}
     * ]
     *
     * @param id
     * @return
     */
    @RequestMapping("/findZnodes")
    @ResponseBody
    public Result findZnodesByParentId(@RequestParam(value = "id", defaultValue = "0") Long id) {

        List<Map<String, Object>> childrenList = dictService.findZnodesByParentId(id);
        Result<List<Map<String, Object>>> ok = null;
        try {
            ok = Result.ok(childrenList);
        } catch (Exception e) {
            System.out.println("e = " + e);
        }
        return ok;  //  转换为JSON数据给是返回给前端
    }


    /**
     * 根据父节点id查询子节点集合
     *
     * @param id 父节点id
     * @return 子节点集合
     */
    @RequestMapping("/findListByParentId/{id}")
    @ResponseBody
    public Result findListByParentId(@PathVariable("id") Long id) {
        List<Dict> childrenList = dictService.findListByParentId(id);
        return Result.ok(childrenList);
    }

    /**
     * 根据字典编码查询该节点的子节点集合
     *
     * @param dictCode 字典编码
     * @return 子节点集合
     */
    @RequestMapping("/findListByDictCode/{dictCode}")
    @ResponseBody
    public Result findListByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> childrenList = dictService.findListByDictCode(dictCode);
        return Result.ok(childrenList);
    }


}
